package com.ylt.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ylt.dao.oracledb.WeChatDao;
import com.ylt.service.WeChatService;
import com.ylt.vo.OrderInfo;
import com.ylt.vo.ProductInfo;

/**
 * @author zhouliang
 * @create 2018-12-03 18:20
 */
@Service
public class WeChatServiceImpl implements WeChatService{

    @Autowired
    WeChatDao weChatDao;

    @Override
    public List<Map> getImageInfo(String shopId) {

        List userDomains = weChatDao.selectImageInfo(shopId);
        //PageInfo result = new PageInfo(userDomains);
        return userDomains;
    }
    @Override
    public List<Map> getYlt_news() {
        List news = weChatDao.selectYlt_news();
        //PageInfo pageInfo = new PageInfo(news);
        return news;
    }

    @Override
    public Integer binging_wx_check(Map wxMap) {
        Integer count = weChatDao.binding_wx_check(wxMap);
        return count;
    }

    @Override
    public void binging_wx(Map wxMap) {
        weChatDao.binding_wx(wxMap);
    }

    /**
     *
     * @param pageNum
     * @param pageSize
     * @param cust_id
     * @param so_zt
     * @return
     */
    @Override
    public PageInfo<Map> getYlt_orders_header(int pageNum, int pageSize, String cust_id,String so_zt,String pay_state) {
        PageHelper.startPage(pageNum,pageSize);
        Map tempMap = new HashMap();
        tempMap.put("CUST_ID",cust_id);
        if (so_zt!=null && !so_zt.isEmpty()) {
            tempMap.put("CXTJARR",so_zt.split(","));
        }
        tempMap.put("PAY_STATE",pay_state);
        List orderList = weChatDao.selectYlt_orders_header(tempMap);
        PageInfo info = new PageInfo(orderList);
        return info;
    }

    @Override
    public PageInfo<Map> getYlt_orders_LINE(int pageNum, int pageSize, String b2b_order_number) {
        PageHelper.startPage(pageNum,pageSize);
        List order_line_List = weChatDao.selectYlt_orders_line(b2b_order_number);
        PageInfo info = new PageInfo(order_line_List);
        return info;
    }
    
    @Override
    public PageInfo<Map> getYlt_items(Map reqMap) {
    	PageHelper.startPage(Integer.parseInt(reqMap.get("pageNum").toString()),Integer.parseInt(reqMap.get("pageSize").toString()));
        List order_line_List = weChatDao.select_Items(reqMap);
        PageInfo info = new PageInfo(order_line_List);
        return info;
    }

    @Override
    public Map getYlt_customerInfo(String wx_id) {

        Map tempList = weChatDao.selectYlt_customerInfo(wx_id);
        return tempList;
    }

    @Override
    public void ylt_product_registration(Map<String, Object> reqMap) {
        weChatDao.insert_ylt_product_registration(reqMap);
    }

    @Override
    public PageInfo<Map> get_ylt_product_registration(Map reqMap) {
        PageHelper.startPage(Integer.parseInt(reqMap.get("pageNum").toString()),Integer.parseInt(reqMap.get("pageSize").toString()));
        List product_List = weChatDao.get_ylt_product_registration(reqMap);
        PageInfo info = new PageInfo(product_List);
        return info;
    }

    @Override
    public void ylt_cancle_registration(Map wxMap) {
        weChatDao.ylt_cancle_registration(wxMap);
    }

    @Override
    public void ylt_signOrder(Map<String, Object> reqMap) {
        weChatDao.ylt_signOrder(reqMap);
    }
    
    @Override
    public PageInfo<Map> select_ItemPrice(Map reqMap) {
    	PageHelper.startPage(Integer.parseInt(reqMap.get("pageNum").toString()),Integer.parseInt(reqMap.get("pageSize").toString()));
        List price_List = weChatDao.select_ItemPrice(reqMap);
        PageInfo info = new PageInfo(price_List);
        return info;
    }

    @Scheduled(cron="0 59 23 * * *")
    public void creatYLT_CUSTOMER_HZ(){

        System.out.println("执行了。。。。。");
        weChatDao.creatYLT_CUSTOMER_HZ();
    }
    /**
     *获取发运头列表
     */
    @Override
    public PageInfo<Map> getYlt_delivers_header_fk(Map reqMap) {
        PageHelper.startPage(Integer.parseInt(reqMap.get("pageNum").toString()),Integer.parseInt(reqMap.get("pageSize").toString()));
        List deliveList = weChatDao.selectYlt_delivers_header_fk(reqMap);
        //封装订单List
        List<OrderInfo> orderInfoList=new ArrayList<OrderInfo>();
        for(int i=0;i<deliveList.size();i++){
            Map orderMap = (Map) deliveList.get(i);
            String b2b_order_number=orderMap.get("B2B_ORDER_NUMBER").toString();
           /* String so_zt=orderMap.get("SO_ZT").toString();
            String pay_state=orderMap.get("PAY_STATE")==null?"":orderMap.get("PAY_STATE").toString();
            int state=1;
            if(("23".equals(so_zt)||"5".equals(so_zt)||"6".equals(so_zt)||"7".equals(so_zt))&&"".equals(pay_state))
            {
                state=1;
            }
            if(("23".equals(so_zt)||"5".equals(so_zt))&&"".equals(pay_state))
            {
                state=2;
            }
            if("6".equals(so_zt)&&"".equals(pay_state))
            {
                state=3;
            }
            if(("6".equals(so_zt)||"7".equals(so_zt))&&"".equals(pay_state))
            {
                state=4;
            }
            if(("23".equals(so_zt)||"5".equals(so_zt)||"6".equals(so_zt)||"7".equals(so_zt))&&"Y".equals(pay_state))
            {
                state=5;
            }

           */
            OrderInfo orderInfo=new OrderInfo();
            orderInfo.setId(Integer.parseInt(b2b_order_number));//订单ID
           // orderInfo.setState(state);//1=全部 2=待发货 3=待收货 4=代付款 5=已完成
            orderInfo.setTime(orderMap.get("ORDER_TJSJ").toString());//订单创建时间
            orderInfo.setOrderProductCount(Integer.parseInt(orderMap.get("ZJS").toString()));//订单商品总数量
            orderInfo.setOrderProductTotal(orderMap.get("ORDER_ZE").toString());//订单总金额
            //封装订单商品List
            Map lineOMap=new HashMap();
            lineOMap.put("B2B_ORDER_NUMBER",orderMap.get("B2B_ORDER_NUMBER").toString());
           // String data_type=orderMap.get("DATA_TYPE").toString();

            List productList = weChatDao.selectYlt_fk_item_yf(lineOMap);

            List<ProductInfo> productInfoList=new ArrayList<ProductInfo>();
            for(int ii=0;ii<productList.size();ii++){
                Map lineMap = (Map) productList.get(ii);
                ProductInfo productInfo=new ProductInfo();
                String title=lineMap.get("T_ITEM_NAME").toString()+"("+lineMap.get("T_JIXING").toString()+")";
                String attr=lineMap.get("T_SPEC").toString()+"/"+lineMap.get("BZDW").toString()+"/"+lineMap.get("T_FACTORY").toString();

                productInfo.setTitle(title);//商品标题
                productInfo.setNumber(Integer.parseInt(lineMap.get("ORDER_QTY").toString()));//商品数量
                productInfo.setAttr(attr);//商品耐属性
                productInfo.setImage("");//商品图片URL
                productInfo.setPrice(lineMap.get("UNIT_PRICE").toString());//商品价格
                productInfoList.add(productInfo);
            }
            orderInfo.setGoodsList(productInfoList);
            orderInfoList.add(orderInfo);
        }

        PageInfo info = new PageInfo(orderInfoList);
        return info;
    }
    /**
     *获取订单头列表
     */
    @Override
    public PageInfo<Map> getYlt_orders_header_fk(Map reqMap) {
        PageHelper.startPage(Integer.parseInt(reqMap.get("pageNum").toString()),Integer.parseInt(reqMap.get("pageSize").toString()));
        List orderList = weChatDao.selectYlt_orders_header_fk(reqMap);
        //封装订单List
        List<OrderInfo> orderInfoList=new ArrayList<OrderInfo>();
        for(int i=0;i<orderList.size();i++){
            Map orderMap = (Map) orderList.get(i);
            String b2b_order_number=orderMap.get("B2B_ORDER_NUMBER").toString();
         /*   String so_zt=orderMap.get("SO_ZT").toString();
            String pay_state=orderMap.get("Pay_STATE").toString();
            int state=1;
            if(("23".equals(so_zt)||"5".equals(so_zt)||"6".equals(so_zt)||"7".equals(so_zt))&&"".equals(pay_state))
            {
                state=1;
            }
            if(("23".equals(so_zt)||"5".equals(so_zt))&&"".equals(pay_state))
            {
                state=2;
            }
            if("6".equals(so_zt)&&"".equals(pay_state))
            {
                state=3;
            }
            if(("6".equals(so_zt)||"7".equals(so_zt))&&"".equals(pay_state))
            {
                state=4;
            }
            if(("23".equals(so_zt)||"5".equals(so_zt)||"6".equals(so_zt)||"7".equals(so_zt))&&"Y".equals(pay_state))
            {
                state=5;
            }

          */
            OrderInfo orderInfo=new OrderInfo();
            orderInfo.setId(Integer.parseInt(b2b_order_number));//订单ID
          //  orderInfo.setState(state);//1=全部 2=待发货 3=待收货 4=代付款 5=已完成
            orderInfo.setTime(orderMap.get("ORDER_TJSJ").toString());//订单创建时间
            if(orderMap.get("ZJS")!=null&&orderMap.get("ZJS").toString().indexOf(".")>-1) {
            	orderInfo.setOrderProductCount(Integer.parseInt(orderMap.get("ZJS").toString().split("\\.")[0]));//订单商品总数量
            }else if(orderMap.get("ZJS")!=null){
            	orderInfo.setOrderProductCount(Integer.parseInt(orderMap.get("ZJS").toString()));//订单商品总数量
            }
            orderInfo.setOrderProductTotal(orderMap.get("ORDER_ZE").toString());//订单总金额
            //封装订单商品List
            Map lineOMap=new HashMap();
            lineOMap.put("B2B_ORDER_NUMBER",orderMap.get("B2B_ORDER_NUMBER").toString());
            String data_type=orderMap.get("DATA_TYPE").toString();

            List   productList = weChatDao.selectYlt_fk_item(lineOMap);

            List<ProductInfo> productInfoList=new ArrayList<ProductInfo>();
            for(int ii=0;ii<productList.size();ii++){
                Map lineMap = (Map) productList.get(ii);
                ProductInfo productInfo=new ProductInfo();
                String title=lineMap.get("T_ITEM_NAME").toString()+"("+lineMap.get("T_JIXING").toString()+")";
                String attr=lineMap.get("T_SPEC").toString()+"/"+lineMap.get("BZDW").toString()+"/"+lineMap.get("T_FACTORY").toString();

                productInfo.setTitle(title);//商品标题
                productInfo.setNumber(Integer.parseInt(lineMap.get("ORDER_QTY").toString()));//商品数量
                productInfo.setAttr(attr);//商品耐属性
                productInfo.setImage("");//商品图片URL
                productInfo.setPrice(lineMap.get("UNIT_PRICE").toString());//商品价格
                productInfoList.add(productInfo);
            }
            orderInfo.setGoodsList(productInfoList);
            orderInfoList.add(orderInfo);
        }

        PageInfo info = new PageInfo(orderInfoList);
        return info;
    }

    /**
     * 获取订单明细列表
     * @return
     */
    @Override
    public PageInfo<Map> getYlt_Order_Item(Map reqMap) {
        PageHelper.startPage(Integer.parseInt(reqMap.get("pageNum").toString()),Integer.parseInt(reqMap.get("pageSize").toString()));
        List order_line_List = weChatDao.selectYlt_fk_item(reqMap);
        PageInfo info = new PageInfo(order_line_List);
        return info;
    }
    /**
     * 获取订单明细列表
     * @return
     */
    @Override
    public PageInfo<Map> getYlt_Deliver_Item(Map reqMap) {
        PageHelper.startPage(Integer.parseInt(reqMap.get("pageNum").toString()),Integer.parseInt(reqMap.get("pageSize").toString()));
        List order_line_List = weChatDao.selectYlt_fk_item_yf(reqMap);
        PageInfo info = new PageInfo(order_line_List);
        return info;
    }

    @Override
    public void update_Order_Header_ZT(Map wxMap){
        weChatDao.update_Order_Header_ZT(wxMap);
    }
    
    @Override
    public Map<String,Object> selectOrderPayInfo(Map reqMap){
    	return weChatDao.selectOrderPayInfo(reqMap);
    }
}
